package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.entities.TimeIntervall;
import cm.mefobe.android.telecomcost.entities.TimePeriod;
import android.content.Context;
import android.graphics.Canvas;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TimeIntervallView extends TableLayout {

	protected Context _context;
	protected TextView _label;
	protected EditText _txtOccurences;
	protected TimePeriodPickerView periodPicker;
	protected TableRow row;
	private TimeIntervall _timeIntervall;

	public TimePeriod getPeriod() {
		return periodPicker.getPeriod();
	}

	public void setPeriod(TimePeriod period) {
		_timeIntervall.setPeriod(period);
		periodPicker.setPeriod(period);
	}

	public boolean isTimeIntervallSet() {
		return _timeIntervall != null;
	}

	public TimeIntervall getTimeIntervall() {
		return new TimeIntervall(periodPicker.getPeriod(), Integer.parseInt(_txtOccurences.getText().toString().trim()));
	}

	public void setTimeIntervall(TimeIntervall timeIntervall) {
		_timeIntervall = timeIntervall;
		setOccurences(timeIntervall.getOccurence());
		setPeriod(timeIntervall.getPeriod());
	}
	
	public void setTimeIntervall() {
		int occurences = Integer.parseInt(_txtOccurences.getText().toString().trim());
		_timeIntervall.setOccurence(occurences);
		setOccurences(_timeIntervall.getOccurence());
//		setPeriod(_timeIntervall.getPeriod());
	}

	private void setOccurences(int occurence) {	
		String text = Integer.toString(occurence);
		while(text.length() < 6)
		{
			text = text + " ";
		}
		
		_txtOccurences.setText(text);		
	}

	public TimeIntervallView(Context context, String label) {
		super(context);
		Log.i("TimeIntervallView", "creating the time picker view for " + label);
		_context = context;
		_label.setText(label);
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	protected void setTimeIntervallView() {
		if (isInEditMode()) {
			Log.i("TimeIntervallView", "isInEditMode");
		} else {
			if (row == null) {
				Log.i("TimeIntervallView", "adding views...");
				row = new TableRow(_context);
				_label = new TextView(_context);
				_txtOccurences = new EditText(_context);
				periodPicker = new TimePeriodPickerView(_context);

				_label.setText("Free Time");
				_txtOccurences.setInputType(InputType.TYPE_CLASS_NUMBER);
				_txtOccurences.setText("0    ");

				row.addView(_label);
				row.addView(_txtOccurences);
				row.addView(periodPicker);

				addView(row);
				setColumnStretchable(2, true);
				setTimeIntervall(new TimeIntervall(TimePeriod.SECOND, 0));
			} else {
				Log.i("TimeIntervallView", "TimeIntervallView already set");
			}
		}
	}
	
	public EditText getEditText()
	{
		return _txtOccurences;
	}
	
	public TimePeriodPickerView getPicker()
	{
		return periodPicker;
	}

	public TimeIntervallView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("TimeIntervallView",
				"creating the time picker view with AttributeSet");

		_context = context;
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	public TimeIntervallView(Context context) {
		super(context);
		Log.i("TimeIntervallView", "creating the time picker view");
		_context = context;
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("TimeIntervallView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("TimeIntervallView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("TimeIntervallView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("TimeIntervallView", "onFinishInflate");
		super.onFinishInflate();
		setTimeIntervallView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("TimeIntervallView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label != null) {
			_label.setText(label);
		} else {
			Log.i("TimeIntervallView",
					"cannot set the label...textView is null");
		}
	}

	@Override
	public String toString() {		
		return _timeIntervall.getOccurence() + " " + periodPicker.getPeriod().toString();
	}	
}
